<?php
/**
 * 系统处理类
 *
 * @version $Id: System.php 430 2013-11-23 03:23:34Z linsir123 $
 * @package Common
 */
class lpSystem
{
	/**
	 * @var 系统错误类型
	 * @static
	 */
	static $_ERRORS = array(
		E_ERROR => 'ERROR',
		E_WARNING => 'WARNING',
		E_PARSE => 'PARSING ERROR',
		E_NOTICE => 'NOTICE',
		E_CORE_ERROR => 'CORE ERROR',
		E_CORE_WARNING => 'CORE WARNING',
		E_COMPILE_ERROR => 'COMPILE ERROR',
		E_COMPILE_WARNING => 'COMPILE WARNING',
		E_USER_ERROR => 'USER ERROR',
		E_USER_WARNING => 'USER WARNING',
		E_USER_NOTICE => 'USER NOTICE',
		E_STRICT => 'STRICT NOTICE',
		E_RECOVERABLE_ERROR => 'RECOVERABLE ERROR',
	);

	/**
	 * 自定义错误处理
	 *
	 * @param int $errNum 错误代码
	 * @param string $errStr 错误内容
	 * @param string $errFile 错误文件
	 * @param int $errLine 错误文件行号
	 * @param array $errContext 错误上下文信息
	 *
	 * @static
	 */
	static function errorHandler($errNum, $errStr, $errFile, $errLine, $errContext)
	{
		$excludes = array(E_NOTICE, E_STRICT, E_USER_NOTICE);

		///
		if (in_array($errNum, $excludes))
			return;

		///
		$type = array_key_exists($errNum, self::$_ERRORS) ?
			self::$_ERRORS[$errNum] : 'CAUGHT EXCEPTION';

		$content = sprintf('[%s] %s (%s, %s)', $type, $errStr, $errFile, $errLine);

		///
		App::log($content, 'runtime/error');
	}

	/**
	 * 自定义异常处理
	 *
	 * @param Exception $e 异常对象
	 *
	 * @static
	 */
	static function exceptionHandler($e)
	{
		try {
			switch ($e->getCode()) {
				case 404:
					$file = 'miss';
					$sysCustomCtrl = App::O('main.controllerMiss');
					break;

				default:
					$file = 'debug';
					$sysCustomCtrl = App::O('main.controllerDebug');

					///
					$content = sprintf('%s [%s, %s]',
						$e->getMessage(),
						$e->getFile(),
						$e->getLine());

					App::log($content, 'runtime/exception');
					break;
			}

			/// 1.1
			if (! empty($sysCustomCtrl)) {
				$data = is_array($sysCustomCtrl) ? $sysCustomCtrl : array($sysCustomCtrl);
				App::getDispatcher()->gotoCtrl($data[0], $data[1], false);
			}

			/// 1.2
			elseif (App::O('main.debug')) {
				$file = CORE_DIR . 'View/tpl/' . $file . '.php';
				require($file);
			}

		} catch (Exception $e) {

			/// 2
			$file = CACHE_DIR . sprintf("%s-error_log", date("Y_m_d"));
			$content = date('Y-m-d H:i:s') . ": " . $e->getMessage() . "\r\n";
			echo "Lxsphp2.X [exceptionHandler]: ", $content;
			error_log($content, 3, $file);
		}

		///
		App::end();
	}

	/**
	 * 输出无缓存头部
	 *
	 * @static
	 */
	static function hNocache()
	{
		header("Pragma: no-cache");
		header("Cache-Control: no-cache, must-ridate");
	}

	/**
	 * 输出字符集
	 *
	 * @param string $charset 字符集
	 * @param string $type 输出格式
	 *
	 * @static
	 */
	static function hCharset($charset, $type = 'text/html')
	{
		header("Content-Type:" . $type . ";charset=" . $charset);
	}

	/**
	 * 输出301，永久重定向
	 *
	 * @param string $location 重定向地址
	 *
	 * @static
	 */
	static function h301($location)
	{
		header("HTTP/1.1 301 Moved Permanently");
		header("Location: " . $location);
	}

	/**
	 * 输出304，找不到更新
	 * // 用于取得浏览器的$_SERVER['HTTP_IF_MODIFIED_SINCE']，
	 * // 进行对比判断页面是否有更新，如果没则直接返回304输出
	 *
	 * @param int $expire 过期时间
	 *
	 * @static
	 */
	static function h304($expire = 3600)
	{
		if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
			$timeLast = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
			if ($timeLast + $expire > time()) {
				header("HTTP/1.1 304 Not Modified");
				exit();
			}
		}

		///
		header("Last-Modified:" . date('F j, Y, g:i a'));
	}

	/**
	 * 输出404，找不到页面
	 *
	 * @static
	 */
	static function h404()
	{
		header("HTTP/1.1 404 Not Found");
	}
}
